﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hengbei.BLL;
using Hengbei.DAL.Models;
using System.Data;

namespace Hengbei.Web
{
    public partial class EditArticle : System.Web.UI.Page
    {
        private Article article;

        public Article ArticleModel
        {
            get { return article; }
            set { article = value; }
        }

        public string ContentText
        {
            get
            {
                return HttpUtility.HtmlDecode(contentTextBox.Text.Trim());
            }
            set
            {
                contentTextBox.Text = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int id;
            if (int.TryParse(Request.QueryString["id"].ToString(), out id))
            {
                ArticleModel = ArticleService.GetArticleByID(id);
                if (id == 0)
                {
                    ArticleModel.EntityState = EntityState.Added;
                    pnlEditArticle.Visible = false;
                    dvArticle.Visible = true;
                }
                else
                {
                    ArticleModel.EntityState = EntityState.Modified;
                    pnlEditArticle.Visible = true;
                    dvArticle.Visible = false;
                    if (!IsPostBack)
                    {
                        txtTitle.Text = ArticleModel.Title;
                        ddlCategories.SelectedValue = ArticleModel.CategoryID.ToString();
                        contentTextBox.Text = ArticleModel.Content;
                    }
                }
                if (IsPostBack)
                {
                    if (contentTextBox != null && !string.IsNullOrWhiteSpace(contentTextBox.Text))
                    {
                        contentTextBox.Text = HttpUtility.HtmlDecode(contentTextBox.Text.Trim());
                    }
                }
            }
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ArticleManage.aspx");
        }

        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {
            //int userID = dc.isLogin();
            ////userID = 1001;
            //if (userID != 0)
            //{
            //    string sql = "INSERT INTO [dbo].[article]([userID],[artName],[artCon],[artTime],[logPer],[sortID]) values (" + userID + ",'" + this.titleTextBox.Text.Trim() + "','" + contentTextBox.Text.Trim().Replace("'", "’") + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',null," + int.Parse(this.categoryDropDownList.SelectedValue) + ")";
            //    System.Diagnostics.Debug.WriteLine(sql);
            //    if (dc.GetNonQuery(sql) < 1)
            //    {
            //        
            //    }
            //    else
            //    {
            //        LabelStatus.Text = "保存文章成功";
            //    }
            //}
            ArticleModel.Title = txtTitle.Text.Trim();
            ArticleModel.CategoryID = int.Parse(ddlCategories.SelectedValue);
            //ArticleModel.Body = contentTextBox.Text.Trim();
            ArticleModel.Content = ContentText;
            ArticleModel.LastModifyDate = System.DateTime.Now;
            ArticleModel.UserID = 1;
            int result = ArticleService.SaveArticle(ArticleModel);
            if (result < 1)
            {
                LabelStatus.Text = "保存文章失败";
            }
            else
            {
                LabelStatus.Text = "保存文章成功";
            }
        }

        protected void dvArticle_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            e.Values["Body"] = HttpUtility.HtmlDecode(e.Values["Body"].ToString());
            e.Values["CreateDate"] = DateTime.Now;
        }

        protected void dvArticle_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            Response.Redirect("ArticleManage.aspx");
        }
    }
}